/**
 * 
 */
package langnstats.project.languagemodel.loglinear;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import langnstats.project.lib.WordType;

/**
 * The abstracted feature class
 * @author Qin Gao
 *
 */
public abstract class LLFeature implements Serializable{
	private static List<LLFeature> allFeatures = new LinkedList<LLFeature>();
	
	public static void addFeature(LLFeature feat){
		allFeatures.add(feat);
	}
	
	public static List<LLFeature> getFeatures(){
		return allFeatures;
	}
	
	public static void rewindAll(){
		Iterator<LLFeature> it = allFeatures.iterator();
		while(it.hasNext()){
			it.next().rewind();
		}
		
	}
	
	public static void removeAllFeatures(){
		allFeatures.clear();
	}
	
	public static String buildFeatureString(WordType token){
		Iterator<LLFeature> it = allFeatures.iterator();
		StringBuffer str = new StringBuffer();
		while(it.hasNext()){
			str.append(it.next().getFeature(token)+" ");
			
		}
		return str.toString();
	}
	
	/**
	 * When a new training/testing is going on, this function is invoked
	 * so you can do some clearing work
	 */
	public abstract void rewind();
	/**
	 * Get the feature string, given the current token token, and implementation
	 * should generate string without space, and keep track of history.
	 * @param token
	 * @return
	 */
	public abstract String getFeature(WordType token);
}
